﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ServiceModel;
using System.Diagnostics;
using System.Runtime.Serialization;
using System.Collections;
using System.Data;
using System.Data.SqlClient;
using ReflectionStudio.Core.Events;
using System.Data.OracleClient;
namespace WPFExcelReport
{

    public class PatternMasterData : BaseFunction
    {


        public PatternMasterData()
            : base()
        {

        }
        public PatternMasterData(OracleConnection _myConnection, IntexReptServBLClient _proxy)
            : base(_myConnection, _proxy)
        {

        }
        public DataSet LoadTableStructure(DataSet dsTarget)
        {
            TableQueue.Enqueue("WTLEIST");
            TableQueue.Enqueue("SELECT *"
                + "  FROM INTEX.WTLEIST WHERE RowNum<1");
            try
            {
                while (TableQueue.Count > 0)
                {
                    string TableName = TableQueue.Dequeue() as string;
                    string QueryStr = TableQueue.Dequeue() as string;
                    orlReadCommand = OracleDB.CreateCommand();
                    orlReadCommand.CommandText = QueryStr;
                    dsResult = Connect2Proxy(orlReadCommand);
                    if (dsResult.Tables.Count > 0)
                    {
                        dsResult.Tables[0].TableName = TableName;
                    }
                    foreach (DataTable dtTemp in dsResult.Tables)
                    {
                        dsTarget = ImportDataTable(dsTarget, dtTemp);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }
        public PatternMasterData(Dictionary<string, object> DicGlobalPara)
            : base(DicGlobalPara)
        {
        }
        public DataSet GetPatternMaster(DataSet dsTarget, DataRow dwPattern)
        {
            try
            {
                string Client = "00";
                string Lang = "V";
                string PatternTable = dwPattern["PatternTable"].ToString();
                string PatternType = dwPattern["PatternType"].ToString();
                string PatternKeyName = dwPattern["PatternKeyName"].ToString();
                string PatternKey = dwPattern["PatternKey"].ToString() + "                                            ";

                orlReadCommand = OracleDB.CreateCommand();
                if (PatternTable == "SDDAT")
                {
                    PatternKey = Client + PatternKey.Substring(0, 21) + Lang;
                    orlReadCommand.CommandText = "SELECT * FROM INTEX."
                 + PatternTable + " WHERE SDSA='0109' AND " +
                 PatternKeyName + " ='" + PatternKey + "'";
                    //select * from sddat where SDSA='0109' AND SDKEY Like '%4226%V'
                }
                else
                {
                    if (PatternTable == "WTSCHUSS"
                        || PatternTable == "WTSAER"
                        )
                    {
                        PatternKey = PatternKey + "               ";
                        PatternKey = PatternKey.Substring(0, 15);
                    }
                    orlReadCommand.CommandText = "SELECT * FROM INTEX."
                       + PatternTable + " WHERE " +
                       PatternKeyName + "='" + PatternKey + "'";
                }
                dsResult = Connect2Proxy(orlReadCommand);
                if (dsResult.Tables.Count > 0)
                {
                    dsResult.Tables[0].TableName = PatternTable;
                    dsResult.Tables[PatternTable].Columns.Add("COMMENTCN", typeof(String));
                }
                dsResult = AddComments(dsResult);
                foreach (DataTable dtTemp in dsResult.Tables)
                {
                    dsTarget.Merge(dtTemp);
                }

            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }
        private DataSet AddComments(DataSet dsSource)
        {
            try
            {
                string Txtkey = "";
                string txtitxtar = "SFG";
                foreach (DataRow dwTemp in dsSource.Tables[0].Rows)
                {
                    if (dwTemp.Table.Columns.Contains("WTBSRFNR"))
                    {
                        Txtkey = dwTemp["WTBSRFNR"].ToString().Trim();
                    }
                    if (dwTemp.Table.TableName == "SDDAT")
                    {
                        txtitxtar = "SDF";
                        Txtkey = dwTemp["SDKEY"].ToString().Trim();
                        Txtkey = "0109" + Txtkey.Substring(2, 22);
                    }
                    if (Txtkey != "")
                    {
                        dwTemp["COMMENTCN"] = GetCommentsFromTXTIND(txtitxtar, Txtkey);
                    }
                }
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsSource;
        }
        public DataSet BuildPatternMasterData(DataSet dsTarget, DataRow dwPattern)
        {
            try
            {
                dsTarget = GetPatternMaster(dsTarget, dwPattern);
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
            return dsTarget;
        }
    }
}